package com.qiwx.pigeon.activity.account;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.Utils.SharedPreferencesUtil;
import com.qiwx.pigeon.Utils.StringUtil;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.vo.EmployBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.presenter.UserManagerPresenter;
import com.qiwx.pigeon.viewinterface.IUserManagerView;
import com.qiwx.pigeon.widget.ArrowView;

import org.angmarch.views.NiceSpinner;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-06-06 11:11
 * 添加用户
 **/
public class AddUserActivity extends BaseMVPActivity<IUserManagerView, UserManagerPresenter> implements IUserManagerView {
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.edt_name)
    EditText edtName;
    @BindView(R.id.edt_pwd)
    EditText edtPwd;
    @BindView(R.id.spinner_role)
    NiceSpinner spinnerRole;
    @BindView(R.id.title_content)
    TextView titleContent;
    List<String> roles = new ArrayList<>();
    @BindView(R.id.title_right)
    TextView titleRight;
    EmployBean employBean;

    @Override
    public UserManagerPresenter createPresenter() {
        return new UserManagerPresenter(new WeakReference(this));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_user;
    }

    @Override
    public void initAfterOnCreate() {
        employBean = SharedPreferencesUtil.getEmployBean(this);
        if (employBean == null){
            MyToast.showShort("重新登录");
            finish();
        }
        initView();

    }

    private void initView() {
        titleReturn.changeArrowLeft();
        titleContent.setText("添加账号");
        titleRight.setText("保存");
        titleRight.setVisibility(View.VISIBLE);
        roles.clear();
        roles.add("管理员");
        roles.add("普通员工");
        spinnerRole.attachDataSource(roles);
        spinnerRole.setOnSpinnerItemSelectedListener((parent, view, position, id) -> {

        });
        spinnerRole.setSelectedIndex(1);
    }


    @Override
    public void onMemberSuccess(PageInfo<EmployBean> datas) {

    }

    @Override
    public void onSuccess(int code) {
        MyToast.showShort("添加成功");
        finish();

    }

    @Override
    public void onFailure(String error) {

    }

    private void save() {
        if (checkParam()) {
            EmployBean param = new EmployBean();
            param.setF_id(employBean.getF_id());
            if (spinnerRole.getSelectedIndex() == 0) {//管理员
                param.setRole(1);
                param.setS_id(0);
            } else {
                param.setRole(0);
                param.setS_id(employBean.getId());
            }
            param.setLogin_name(edtName.getText().toString());
            param.setPwd(edtPwd.getText().toString());
            mPresenter.addEmployer(param);
        }

    }

    private boolean checkParam() {
        if (employBean == null) {
            MyToast.showShort("重新登录");
            return false;
        }
        if (StringUtil.isEmpty(edtName.getText().toString())) {
            MyToast.showShort("用户名不能为空");
            return false;
        }
        if (StringUtil.isEmpty(edtPwd.getText().toString())) {
            MyToast.showShort("密码不能为空");
            return false;
        }
        return true;

    }


    @OnClick({R.id.title_return, R.id.title_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_return:
                finish();
                break;
            case R.id.title_right:
                save();
                break;
        }
    }

}
